package web;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.User;
import service.UserService;
import service.userserviceimp;

/**
 * Servlet implementation class modifyservlet
 */
public class modifyservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserService userservice=new userserviceimp();
       


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session=request.getSession();
		String oldusername=(String) session.getAttribute("username");
		String reusername=request.getParameter("reusername");
		String repassword=request.getParameter("repassword");
		User newuser=new User(null,reusername,repassword);
		User olduser=new User(null,oldusername,null); 
		if(userservice.asks(reusername))
		{
		if(userservice.update(newuser,olduser)==true){
			Enumeration<?> em = request.getSession().getAttributeNames();
			while(em.hasMoreElements()){
				request.getSession().removeAttribute(em.nextElement().toString());
			}
			request.getRequestDispatcher("/modifysuccess.html").forward(request, response);
		}
		else{
			System.out.println("修改失败");
		}
		}
		else{
			System.out.println("用户名已被注册");
			request.getRequestDispatcher("/modify.jsp").forward(request, response);
		}
	}

}
